package jzoffer;

/**
 * @author MaoLin Wang
 * @date 2020/10/12 2:07 下午
 */
public class _39数组中出现次数大于一半的数字 {
    public static int majorityElement(int[] nums) {
        if (nums.length==1){
            return nums[0];
        }
        int count=1;
        int cur=nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i]!=cur){
                count--;
                if (count<0){
                    count=1;
                    cur=nums[i];
                }
            }else {
                count++;
            }
        }
        return cur;
    }

    public static void main(String[] args) {
        int i = majorityElement(new int[]{6, 5, 5});
        System.out.println(i);
    }
}
